<template>
  <div class="app-container">
    <!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="任务名称" prop="task_name">
        <el-input
          v-model="queryParams.task_name"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item label="机器人名称" prop="robot_name">
        <el-input
          v-model="queryParams.robot_name"
          placeholder="请输入机器人名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="queryParams.create_times"
          style="width: 215px" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ptwn:patroltask:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-folder-checked"
            size="mini"
            :disabled="multiple"
            @click="handleAuto"
            v-hasPermi="['ptwn:patroltask:zdedit']"
        >设置自动</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-folder-delete"
            size="mini"
            :disabled="multiple"
            @click="handleUAuto"
            v-hasPermi="['ptwn:patroltask:qxzdedit']"
        >取消自动</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ptwn:patroltask:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 数据列表区域-->
    <el-table v-loading="loading" :data="patroltaskList" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务名称" align="center" prop="task_name" />
      <el-table-column label="是否自动任务" align="center" prop="is_auto">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.is_auto == 0">否</el-tag>
          <el-tag v-else>是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="机器人名称" align="center" prop="robot_name"/>
      <el-table-column label="执行时刻(H)" align="center" prop="execution_time">
        <template slot-scope="scope">
          <el-tag>{{scope.row.execution_time}}H</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="速度" align="center" prop="speed">
        <template slot-scope="scope">
          <span>{{scope.row.speed}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="task_type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.task_state == 0">未下发</el-tag>
          <el-tag v-else>已执行</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="creater_time" width="170">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creater_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="280">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['robot:patrolpoint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);"
    />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="630px" @close="handleCancle" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机器人" prop="robot_config_id">
              <el-select v-model="form.robot_config_id" clearable  placeholder="请选择机器人" @change="getPoints">
<!--              <el-select v-model="form.robot_config_id" clearable  placeholder="请选机器人">-->
                <el-option
                  v-for="item in robotList"
                  :key="item.id"
                  :label="item.robot_name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="task_name">
              <el-input v-model="form.task_name" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检速度" prop="speed">
              <el-select v-model="form.speed" placeholder="请选择机器人巡检速度">
                <el-option
                    v-for="sp in speeds"
                    :key="sp"
                    :label="sp"
                    :value="sp"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行时刻" prop="execution_time">
              <el-select v-model="form.execution_time" placeholder="请选择执行时刻(小时)" @change="getOptionszxsj" >
                <el-option
                  v-for="sj in zxsk"
                  :key="sj"
                  :label="sj"
                  :value="sj"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容(0-255)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" id="patrolid">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {
    addPatrolTask,
    delPatrolTask,
    getPatrolTask,
    listPatrolTask,
    listPatrolTasks,
    updatePatrolTask,
    AutoPatrolTask, UAutoPatrolTask
  } from "@/api/ptwn/patroltask/patroltask";
  import {listRobotConfig} from "@/api/ptwn/robotconfig/robotconfig";
  import $ from "jquery";
  import "@/assets/styles/robt-bg.css";
  export default {
    data() {
      return {
        execution_time:null, //日期选择
        pickerOption: {
          disabledDate: time => {
            const dateVal = new Date(this.execution_time).getTime();
            if (dateVal) {
              return time.getTime() > dateVal + 1;
            }
          }
        },
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        task_names:[],
        // 选中巡检点数组
        timer:null,//定时器
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        //机器人列表
        robotList:[],
        // 巡检任务表格数据
        patroltaskList: [],
        zxsk:['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'],
        speeds:['240','480','720','960','1200'],
        checkList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 8,
          task_name: undefined,
          task_num: undefined,
          robot_name: undefined,
          execution_time:undefined,
          create_times: [],
        },
        // 表单参数
        form: {},
        // 表单参数
        form1: {},
        // 表单校验
        rules: {
          task_name: [
            { required: true, message: "任务名称不能为空", trigger: "blur" }
          ] ,
          robot_config_id: [
            { required: true, message: "机器人名称不能为空"}
          ],
          execution_time: [
            { required: true, message: "任务执行时刻不能为空"}
          ],
          speed: [
            { required: true, message: "机器人巡检速度不能为空"}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    mounted() {
      this.timer = setInterval(()=>{
        this.getList(); //刷新站点列表
      },9000);//9秒
    },
    beforeDestroy() {
      //清除定时器
      if(this.timer) {
        clearInterval(this.timer); //关闭
      }
    },
    methods: {
      /**自动任务按钮操作 */
      handleAuto(row) {
        const ids = row.id || this.ids;
        const task_names =row.task_name || this.task_names
        this.$modal.confirm('设置自动任务 "' + task_names + '" ？').then(function() {
          return AutoPatrolTask(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("设置成功");
        }).catch(() => {});
      },
      /**取消自动任务按钮操作 */
      handleUAuto(row) {
        const ids = row.id || this.ids;
        const task_names =row.task_name || this.task_names
        this.$modal.confirm('取消自动任务 "' + task_names + '" ？').then(function() {
          return UAutoPatrolTask(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("取消成功");
        }).catch(() => {});
      },
      //自定义对话框关闭方法
      handleCancle () {
        $("#lxsc").css("display","none");
        $("#tlsj").css("display","none");
        $("#patrolid").css("display","");
        this.form.execution_time = undefined;
        this.queryParams.execution_time = undefined;
        this.$refs.form.resetFields()
        this.dialogVisible = false
      },
      //根据机器人id、执行时间查询是否有关联任务
      getOptionszxsj(execution_time) {
        this.queryParams.execution_time = execution_time;//设置站点id条件，查询工艺体
        listPatrolTasks(this.queryParams).then(response => {
          if(typeof(response)!="undefined" && response != null && response.rows.length>0) {
            $("#patrolid").css("display","none");
            this.$modal.confirm1('当前执行时间任务已创建！');
          }else {
            $("#patrolid").css("display","");
          }
        });
      },
      //根据机器人id，查询巡检点
      getPoints(id){
        this.queryParams.robot_config_id = id;//通过机器人id，查询巡检点
      },
      /** 查询机器人列表 */
      getrobotList() {
        listRobotConfig(this.queryParams).then(response => {
          this.robotList = response.rows;
        });
      },
      /** 查询巡检任务列表 */
      getList() {
        this.loading = true;
        listPatrolTask(this.queryParams).then(response => {
          this.patroltaskList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          robot_name:undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.create_times = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.task_names = selection.map(item => item.task_name)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.patrolpointList=[];
        this.reset();
        this.open = true;
        this.title = "新增任务";
        this.getrobotList();//机器人列表
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getPatrolTask(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改任务";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updatePatrolTask(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPatrolTask(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const task_names =row.task_name || this.task_names
        this.$modal.confirm('是否确认删除任务名称为"' + task_names + '"的数据项？').then(function() {
          // this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
          return delPatrolTask(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
    }
  };
</script>
<style rel="stylesheet/scss" lang="scss">
  /**列表添加垂直隐藏滚动条**/
  .tabled{
    height: 550px;
    overflow: hidden;
    overflow-y: scroll;
  }
  .tabled::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
  }
</style>
